/**
 * 
 */











////////////////////------------评论--点赞--加关注--转发------------////////////
$(function(){
    var $timeline_container = $("div[class='timeline-container hidden']");    //关注的动态dom
    var $publish_review = $timeline_container.find("button[class='btn btn-sm btn-info no-radius']");  //评论
    var $attention = $timeline_container.find("i.attention");
    var $praise = $timeline_container.find("i.praise")
    var $forword = $timeline_container.find("i.forword")
	//评论
	$publish_review.click(function(){
		var $review_content = $(this).parent().siblings("input.myreview") ;
		var review_content = $review_content.val();   //发表的类容
		var dynamic_information_id = $(this).parents("div[class='timeline-container']")[0].id;
		
		if(review_content!=""&&review_content!=null){
		 $.ajax({
			 url:"Publish_review",
			 type:"POST",
			 data:{dynamic_information_id:dynamic_information_id,review_content:review_content},
			 async:false,
			 dataType:"json",
			 success:function(data,status){
				 if(data.result==true){
	                    jSuccess("评论成功",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false}); 
	                    $review_content.val("");
	                    window.location.reload();
	                }
	                else{
	                    jError("操作失败",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
	                }
			 }
			 
		 });
	}else{
		jError("评论类容不能为空",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
	}
		 
	});
	
	//关注
    $attention.click(function(){
    	var $attention_number = $(this).siblings("span.attention");
    	var attention_number = parseInt($attention_number.html());
    	var $dynamic = $(this).parents("div[class='timeline-container']");
    	$.ajax({
    		url:"Attention",
    		type:"POST",
    		async:false,
    		data:{dynamic_information_id:$dynamic[0].id},
    		dataType:"json",
    		success:function(data,status){
    			if(data.result==true){
    				 if(data.attentioned==true){
    				 $attention_number.html(attention_number+1);
    				 }else{
    					 jError("您已经关注过该动态了",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
    				 }
    			}else{
    				jError("操作失败",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
    			}
    		}
    	});
    });
	
	//点赞
    $praise.click(function(){
    	var $praise_number = $(this).siblings("span.praise");
    	var praise_number = parseInt($praise_number.html());
    	var $dynamic = $(this).parents("div[class='timeline-container']");
    	
    	$.ajax({
    		url:"Praise",
    		type:"POST",
    		async:false,
    		data:{dynamic_information_id:$dynamic[0].id},
    		dataType:"json",
    		success:function(data,status){
    			if(data.result==true){
    				 if(data.praise==true){
    					 $praise_number.html(praise_number+1);
    				 }else{
    					 jError("您已经点过赞了",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
    				 }
    			}else{
    				jError("操作失败",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
    			}
    		}
    	});
    });
    
    
    
})






////////-------单击我关注的人，查看我关注的人的动态-------////
$(function(){
	var $itemdiv_memberdiv = $("div[class='itemdiv memberdiv hidden']");
	var $attention_nickname = $itemdiv_memberdiv.find("a.user_name");
	
	var $timeline_container = $("div[class='timeline-container hidden']");    //关注的动态dom
	$attention_nickname.click(function(){
		$timeline_container.siblings().remove();
		var $href = $("<a href='javascript:;'>查看关注的人的动态功能暂待开发</a>");
		$href.insertAfter($timeline_container);
		$("span.my_attention_user_dy").html($(this).html()+"的动态");
		/////功能暂待开发
	});
});



///////////////--------------弹出输入/留言/删除---------------////////////////////
$(function(){
	var $itemdiv_memberdiv = $("div[class='itemdiv memberdiv hidden']");
	var $leaving_message = $itemdiv_memberdiv.find("li.leaving_message");
	var $delete_attention = $itemdiv_memberdiv.find("li.delete_attention");
	//留言
	$leaving_message.click(function(){
		var receive_user_account = $(this).parents("div[class='itemdiv memberdiv']")[0].id;
		//alert(receive_user_account);
		x0p({
			title: '输入您的留言',
		    text: null,
		    width: "90%",
		    height: "50%",
		    type: "input",
		    inputType: "text",
		    buttons: [
		              {
		                  type: 'cancel',
		                  text: '取消'
		              },
		              {
		                  type: 'ok',
		                  text: '确认'
		              }
		          ],
		},function(button, text){
			if(button == 'ok') {
				if(text!=""&&text!=null){
					$.post("leaving_msg_to_attention",{receive_user_account:receive_user_account,leaving_content:text},function(data,status){
						if(data.result==true){
							x0p('提示',"留言已发送", 
					                'ok', false);
						}else{
							alert("留言失败");
						}
					},"json");
					
				}else{
					 x0p('提示',"输入不能为空", 
				                'error', false);
				}
	        }
	        if(button == 'cancel') {
	           
	        }
		});
	});
	
	//删除我的关注
	$delete_attention.click(function(){
		var $delete_user_account = $(this).parents("div[class='itemdiv memberdiv']");
		var delete_user_account = $(this).parents("div[class='itemdiv memberdiv']")[0].id;
		
		$.post("delete_attention_user",{delete_user_account:delete_user_account},function(data,status){
			if(data.result==true){
				//删除成功
				$delete_user_account.remove();
				//window.location.reload();
			}else{
				alert("删除数据失败");
			}
		},"json");
	});
	
	
});



////////////////////////-------------读取数据相关----------------//////////////////////////////////////

$(function(){
	
	////////////////////////----------我关注的人------------////////////////////
	var $itemdiv_memberdiv = $("div[class='itemdiv memberdiv hidden']");
    $.get("Attention_user",function(data,status){
	if(data.result==true){
		for(i=0;i<data.my_attention_Array.length;i++){
			var $attention_user = $itemdiv_memberdiv.clone(true);
			$attention_user[0].id=data.my_attention_Array[i].user_account;
			if(data.my_attention_Array[i].user_photo_path!=""&&data.my_attention_Array[i].user_photo_path!=null){
				$attention_user.find("div.user_photo").children("img")[0].src=data.my_attention_Array[i].user_photo_path;
			}else{
				$attention_user.find("div.user_photo").children("img")[0].src ="images/no_photo.jpg";
			}
			$attention_user.find("a.user_name").html(data.my_attention_Array[i].user_nickname);
			$attention_user.removeClass("hidden");
			$attention_user.insertAfter($itemdiv_memberdiv);
			
		}
	}else{
	alert("请求数据出错");	
	}
    },"json");

////////////////////////----------我关注的动态------------////////////////////
    var $timeline_container = $("div[class='timeline-container hidden']");    //关注的动态dom
    var $timeline_label = $("div[class='timeline-label hidden']");   //“我的关注标签”dom
    var $itemdiv_dialogdiv = $("div[class='itemdiv dialogdiv hidden']");    //一条条评论dom
    
    
    $.get("My_attention_dynamic",function(data,status){
   		if(data.result==true){
   			for(i = 0;i<data.my_Array.length;i++){
   				var $my_attention_dy = $timeline_container.clone(true);
   				
   				$my_attention_dy[0].id = data.my_Array[i].dynamic_information_id;
   				if(data.my_Array[i].user_photo_path!=""&&data.my_Array[i].user_photo_path!=null){
   					$my_attention_dy.find("div.at_user_photo").children("img")[0].src = data.my_Array[i].user_photo_path;
   				}else{
   					$my_attention_dy.find("div.at_user_photo").children("img")[0].src = "images/no_photo.jpg";
   				}
   				$my_attention_dy.find("span.nickname").html(data.my_Array[i].user_nickname);
   				$my_attention_dy.find("span.atdy_date").html(data.my_Array[i].dynamic_information_date);
   				$my_attention_dy.find("span.dy_content").html(data.my_Array[i].dynamic_information_content);
   				$my_attention_dy.find("span.attention").html(data.my_Array[i].dynamic_information_attention_number);
   				$my_attention_dy.find("span.forword").html(data.my_Array[i].dynamic_information_forword_number);
   				$my_attention_dy.find("span.review").html(data.my_Array[i].dynamic_information_review_number);
   				$my_attention_dy.find("span.praise").html(data.my_Array[i].dynamic_information_praise_number);
   				if(i == data.my_Array.length-1){
   					$my_attention_dy.find("div[class='timeline-label hidden']").removeClass("hidden");
   				}
   				
   				
   				for(j=0;j<data.my_Array[i].review_Array.length;j++){
   					var review_val = data.my_Array[i].review_Array[j];
   					var $review = $itemdiv_dialogdiv.clone(true);
   					$review[0].id = review_val.review_id;
   					
   					if(review_val.reviewer_photo!=null&&review_val.reviewer_photo!=""){
   						$review.find("div.re_user_photo").children("img")[0].src = review_val.reviewer_photo;
   					}else{
   						$review.find("div.re_user_photo").children("img")[0].src = "images/no_photo.jpg";
   					}
   					$review.find("span.re_date").html(review_val.review_date);
   					$review.find("a.re_user_nickname").html(review_val.reviewer_nickname);
   					$review.find("div.re_content").html(review_val.review_content);
   					
   					$review.removeClass("hidden");
   					$review.insertBefore($my_attention_dy.find("div[class='itemdiv dialogdiv hidden']"));
   				}
   				
   				$my_attention_dy.removeClass("hidden");
   				$my_attention_dy.insertAfter($timeline_container);
   			}
   		}else{
   			alert("请求数据失败");
   		}
    },"json");

    
});

